package com.fzshang.commen.util.data;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

/**
 * @author 冯政尚
 * @Description: 时间戳转换日期格式
 * @date 2019/11/16 14:03
 */
public class Date {

    /**
     * @param date
     * @return 13时间戳转换成日期
     */
    public static String date(Long date){
       SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
       return sdf.format(new java.util.Date(Long.valueOf(date)));
    }

    /**
     * @param date
     * @return 13时间戳转换成日期
     */
    public static String dateYM(Long date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM");
        return sdf.format(new java.util.Date(Long.valueOf(date)));
    }

    /**
     * @param date
     * @return 今年第一天
     * @throws Exception
     */
    public static String getThisYear(String date) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format3 = new SimpleDateFormat("yyyy");
        java.util.Date time = format3.parse(date);
        String time1 = format3.format(time);
        java.util.Date startTime = format.parse(time1 + "-01-01");
        String dates = format.format(startTime);
        System.out.println("今年第一天:"+dates);
        return dates;
    }


    /**
     * @param today
     * @return 今年最后一天
     * @throws ParseException
     */
    public static String getYearLast(String today) throws ParseException {
        SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar c=Calendar.getInstance(Locale.CHINA);
        c.setTime(f.parse(today));
        c.add(Calendar.YEAR, 0);//拨回去年
        c.set(Calendar.DAY_OF_YEAR,c.getActualMaximum(Calendar.DAY_OF_YEAR));//最后一天
        String time = f.format(c.getTime());
        System.out.println("今年最后一天:"+time);
        return time;
    }

    /**
     * @return 当前年的所有月份
     */
    public static List getThisYearFirstDayAndTheLastDay() throws Exception {
        String today= DateUtil.today();
        java.util.Date a = DateUtil.parse(getThisYear(today));
        java.util.Date b = DateUtil.parse(getYearLast(today));
        List<DateTime> list = DateUtil.rangeToList(a,b, DateField.MONTH);
        List list1 = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            list1.add(DateUtil.format(list.get(i),"yyyy-MM"));
        }
        return list1;
    }

}
